package com.ruoyi.recycle.utils.debang;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;


public class ResultDO<T> {
    private T model;

    public T getModel() {
        return this.model;
    }

    private Map<String, Object> extra = Maps.newConcurrentMap();

    private List<Exception> exceptionList = Lists.newArrayList();


    public ResultDO set(String key, Object value) {
        if (StringUtils.isBlank(key) || null == value) {
            throw new IllegalArgumentException("argument is empty");
        }
        this.extra.put(key, value);
        return this;
    }


    public Object get(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return this.extra.get(key);
    }


    public ResultDO<T> setException(Exception exception) {
        return addException(exception);
    }


    public ResultDO<T> setModel(T model) {
        this.model = model;
        return this;
    }


    public ResultDO<T> addException(Exception ex) {
        if (null != ex) {
            this.exceptionList.add(ex);
        }
        return this;
    }


    public ResultDO<T> addExceptionList(List<Exception> exList) {
        if (CollectionUtil.isNotEmpty(exList)) {
            this.exceptionList.addAll(exList);
        }
        return this;
    }


    public boolean isSuccess() {
        return CollectionUtil.isEmpty(this.exceptionList);
    }

    public Exception getException() {
        return CollectionUtil.isNotEmpty(this.exceptionList) ? this.exceptionList.get(0) : null;
    }


    public List<Exception> getExceptionList() {
        return Collections.unmodifiableList(this.exceptionList);
    }
}